package ProductionConsumption;

/**
 * @Author mao
 * @Date 2025/11/22
 * @Description 生产者类
 */
public class Producer implements Runnable {
    private Supermarket supermarket;
    private int id;
    // 每个生产者最多生产10个
    private static final int MAX_PRODUCE = 10;

    public Producer(Supermarket supermarket, int id) {
        this.supermarket = supermarket;
        this.id = id;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < MAX_PRODUCE; i++) {
                supermarket.produce(id);
                Thread.sleep(200); // 模拟生产耗时
            }
            System.out.println("生产者 " + id + " 完成生产任务。");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
